package com.smartlight.godlight.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.smartlight.godlight.R;
import com.smartlight.godlight.constant.GodlApi;
import com.smartlight.godlight.listener.OnItemClickListener;
import com.smartlight.godlight.model.Buddha;
import com.smartlight.godlight.model.Timespan;

import java.util.List;

public class TimespanAdapter extends RecyclerView.Adapter<TimespanAdapter.MyViewHolder> {
    private List<Timespan> mData; // 假设这是你的数据源
    Context context;
    private static OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listenser) {
        onItemClickListener = listenser;
    }
    public TimespanAdapter(Context context, List<Timespan> data) {
        this.context = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.timespan_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        // 佛像名称
        holder.textview.setText(mData.get(position).getName());

        // 佛像图片
        if (position == selectedPosition) {
            holder.textview.setSelected(true);
        } else {
            holder.textview.setSelected(false);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.textview.setSelected(!holder.textview.isSelected());
                setSelectedPosition(position);
                onItemClickListener.onItemClick(position);
            }
        });
    }

    private int selectedPosition = -1; // 初始化为无效值

    public void setSelectedPosition(int position) {
        selectedPosition = position;
        notifyDataSetChanged(); // 通知adapter数据已改变，重新绘制所有项
    }

    @Override
    public int getItemCount() {
        // 返回数据集的大小
        return mData.size();
    }

    static class  MyViewHolder extends RecyclerView.ViewHolder {
        TextView textview;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview = itemView.findViewById(R.id.textview);

            // 监听item点击事件
            // item点击监听
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (onItemClickListener != null) {
//                        onItemClickListener.onItemClick(getAdapterPosition());
//                    }
//                }
//            });
        }
    }
}
